<template>
  <div class="order-header" >
    <div class="select-ul" v-if="listShipping">
      <section  :class="['select-item',current==index?'active':'']"
              v-for="(shipping,index) in listShipping"
              :key="index"
              @click="itemNav(index)" >
        <!-- 门店配送名称自定义 -->
        <span>{{index == 5 && store_delivery ? store_delivery:shipping}}</span>
      </section>
    </div>

    <self-address-dialog
      ref="selfAddressDialog"
      :shipType="String(current)"
      :buyLists="buyLists"
      :selectLastLocation="selectLastLocation"
      :latitude="latitude"
      :longitude="longitude"
      @getSelfAddressDialog="getSelfAddressDialog"
      @getDateTime="getDateTime"
      :visible.sync="selfAddressVisible"></self-address-dialog>
  </div>
</template>

<script>
  import Vue from 'vue'
  import { selfAddressBefore } from '@/api/order/order'
  import selfAddressDialog from '../selfAddressDialog'
  export default Vue.extend({
    components: {
      selfAddressDialog
    },
    props: {
      listShipping: {
        type: [Object, Array]
      },
      buyLists: {
        type: Array
      },
      shippingType: {
        type: [Number, String]
      },
      latitude: {
        type: [Number, String]
      },
      longitude: {
        type: [Number, String]
      },
      selectLastLocation: {
        type: [Number, String]
      },
      store_delivery: {
        type: String
      }
    },
    data() {
      return {
        current: 1,
        selfAddressVisible: true
      }
    },
    watch: {
      shippingType(val) {
        this.current = val
        // this.itemNav(this.current) // 默认先获取一次当前选择快递方式的默认值
      }
    },
    mounted() {
      if (this.shippingType) {
        this.current = this.shippingType
      } else {
        this.current = this.getListShippingfirst(this.listShipping)
        this.$emit('getShippingType', this.current)
      }
      this.itemNav(this.current) // 默认先获取一次当前选择快递方式的默认值
    },
    methods: {
      getListShippingfirst(data) {
        for (var key in data) { return key }
      },
      itemNav(index) {
        this.$parent.getPostFeeflag = true
        this.current = index
        this.$emit('getShippingType', this.current)
        const formData = {
          scanCode: '',
          addressId: '',
          province_id: '',
          city_id: '',
          area_id: '',
          latitude: '',
          longitude: '',
          address_id: '',
          type: index
        }
        const ids = []
        const buyLists = this.buyLists
        const goods_info = {}
        buyLists.forEach(item => {
          ids.push(item.item_id)
          goods_info[item.item_id] = {
            sku_id: item.sku_id,
            num: item.num
          }
        })
        formData.goods_info = goods_info
        formData.item_ids = ids
        // console.log('index', index)
        // if (index == 4 || index == 5) {
          selfAddressBefore(formData).then(res => {
            this.$emit('getSelfAddress', res.data.before[0])
          })
        // }
      },
      // 获取到自提地址
      getSelfAddressDialog(val) {
        this.$emit('getSelfAddress', val)
        this.selfAddressVisible = false
      },
      // 获取日期 时间数据
      getDateTime(val) {
        this.$emit('getDateTime', val)
      }
    }
  })
</script>

<style>

</style>
